//
//  PPAlbumPictureEditView.m
//  pickpic
//
//  Created by 胡文广 on 2017/5/20.
//  Copyright © 2017年 MicroDream. All rights reserved.
//

#import "PPAlbumPictureEditView.h"
#import "PPEbookPreviewCell.h"

#define kPicutureMargin 9
@interface PPAlbumPictureEditView()<UICollectionViewDelegate,UICollectionViewDataSource>

@end
@implementation PPAlbumPictureEditView
static NSString *const ebookID = @"EbookCell";

- (UICollectionView *)collectionView{

    if (!_collectionView) {

        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        //该方法也可以设置itemSize
        CGFloat w = (SCREEN_WIDTH-kPicutureMargin*3-28)/4;
        layout.itemSize =CGSizeMake(w,w);
        // 设置最小行间距
        layout.minimumLineSpacing = kPicutureMargin;
        // 设置垂直间距
        layout.minimumInteritemSpacing = kPicutureMargin;
        //    2.初始化collectionView
        _collectionView = [[UICollectionView alloc]initWithFrame:self.bounds collectionViewLayout:layout];
//        
//        [_collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([PPEbookPreviewCell class]) bundle:nil] forCellWithReuseIdentifier:ebookID];
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        _collectionView.backgroundColor = WHITE;
    }
    return _collectionView;
}

//- (void)setPictures:(NSMutableArray *)pictures{
//
//    _pictures = pictures;
//    
//    
//}
- (void)reloadData{

    [self.collectionView reloadData];
}

- (instancetype)initWithFrame:(CGRect)frame{
    if (self =[ super initWithFrame:frame]) {
        
         [self addSubview:self.collectionView];
        
        [self.collectionView registerNib:[UINib nibWithNibName:NSStringFromClass([PPEbookPreviewCell class]) bundle:nil] forCellWithReuseIdentifier:ebookID];
        
        
//        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadData) name:@"RELOAD_COLLECTIONVIEW" object:nil];
    }
    return self;
}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.pictures.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    PPEbookPreviewCell *cell = (PPEbookPreviewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:ebookID forIndexPath:indexPath];
    cell.closeBtn.tag = indexPath.item;
    cell.img.contentMode = UIViewContentModeScaleAspectFill;
    PPAbstractArtwork *artwork = self.pictures[indexPath.item];
    cell.closeBtn.hidden = !artwork.uuid;
    cell.img.artwork = artwork;
    [cell.closeBtn addTarget:self action:@selector(removePicture:) forControlEvents:UIControlEventTouchUpInside];
    return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{

     PPAbstractArtwork *artwork = self.self.pictures[indexPath.item];
    if (artwork.uuid) {
        if (self.BlockDidSelectPicture) {
            self.BlockDidSelectPicture(artwork,indexPath.item);
        }
    }else{
        if (self.BlockAddPicture) {
            self.BlockAddPicture();
        }
    }
    
}
- (void)layoutSubviews{

    [super layoutSubviews];
    self.collectionView.frame = self.bounds;
}

- (void)removePicture:(UIButton *)remveBtn{

    PPAbstractArtwork *artwork = self.self.pictures[remveBtn.tag];
    
    if (artwork.uuid) {
        if (self.BlockRemovePicture) {
            self.BlockRemovePicture(artwork,remveBtn.tag);
        }
    }else{
        if (self.BlockAddPicture) {
            self.BlockAddPicture();
        }
    }
    
    
    
}
@end
